# -*- coding:utf-8 -*-
import shutil
import numpy as np
from PIL import Image
import os


def than_size(wtf1, wtf2):
    with open(wtf1, "rb") as f1:
        size1 = len(f1.read())
    with open(wtf2, "rb") as f2:
        size2 = len(f2.read())
    if size1 == size2:
        cnm = "大小相同"
    else:
        cnm = "大小不同"
    return cnm


def than_shape(rnm, knm):
    image1 = Image.open(rnm)
    image2 = Image.open(knm)
    if image1.size == image2.size:
        homo114514 = "尺寸相同"
    else:
        homo114514 = "尺寸不同"
    return homo114514


def men(gay, homo):
    image1 = np.array(Image.open(gay))
    image2 = np.array(Image.open(homo))
    if np.array_equal(image1, image2):
        fuck = "内容相同"
    else:
        fuck = "内容不同"
    return fuck


def sb(tmd, sby):
    ysl = "两张图不同"
    jz = than_size(tmd, sby)
    if jz == "大小相同":
        yj = than_shape(tmd, sby)
        if yj == "尺寸相同":
            nm = men(tmd, sby)
            if nm == "内容相同":
                ysl = "两张图相同"
    return ysl


def main(a111, b11):
    load_path = a111
    save_path = b11
    os.makedirs(save_path, exist_ok=True)
    file_map = {}
    for parent, dir_names, filenames in os.walk(load_path):
        for filename in filenames:
            image_size = os.path.getsize(os.path.join(parent, filename))
            file_map.setdefault(os.path.join(parent, filename), image_size)
    file_map = sorted(file_map.items(), key=lambda d: d[1], reverse=False)
    file_list = []
    for filename, image_size in file_map:
        file_list.append(filename)
    file_repeat = []
    for currIndex, filename in enumerate(file_list):
        dir_image1 = file_list[currIndex]
        dir_image2 = file_list[currIndex + 1]
        result = sb(dir_image1, dir_image2)
        if result == "两张图相同":
            file_repeat.append(file_list[currIndex + 1])
            print("相同的图片：", file_list[currIndex], file_list[currIndex + 1])
        else:
            print('不同的图片：', file_list[currIndex], file_list[currIndex + 1])
        currIndex += 1
        if currIndex >= len(file_list) - 1:
            break
    for image in file_repeat:
        shutil.move(image, save_path)
        print("正在移除重复照片：", image)
